package com.zone.sdk.module.comment;

import com.zone.sdk.common.http.base.BaseCloudServiceManager;
import com.zone.sdk.common.http.base.BaseRequest;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk.module.comment.base.CommentPage;
import com.zone.sdk.module.comment.entity.AppCommentScoreEntity;
import com.zone.sdk.module.comment.entity.CommentListEntity;
import com.zone.sdk.module.comment.entity.RecommendListEntity;
import com.zone.sdk.module.comment.entity.SearchResultEntity;
import com.zone.sdk.module.comment.http.CommentOkHttpManager;
import com.zone.sdk.module.comment.request.AppSearchRequest;
import com.zone.sdk.module.comment.request.CommentDetailRequest;
import com.zone.sdk.module.comment.request.CommentListRequest;
import com.zone.sdk.module.comment.request.CommentScoreRequest;
import com.zone.sdk.module.comment.request.RecommendListRequest;

import org.jetbrains.annotations.NotNull;

import io.reactivex.observers.DisposableObserver;

public class CloudCommentServiceManager extends BaseCloudServiceManager {

    public static CloudCommentServiceManager instance;

    private final ICloudCommentService iCloudCommentService;


    private CloudCommentServiceManager(String baseUrl) {
        super();
        iCloudCommentService = CommentOkHttpManager.getInstance(baseUrl).build(ICloudCommentService.class);
    }

    public static CloudCommentServiceManager getInstance(String baseUrl){
        if (instance == null){
            instance = new CloudCommentServiceManager(baseUrl);
            return instance;
        }
        return instance;
    }


    /**********************************搜索、评论模块*********************************************/

    /**
     * 车机端同步下载队列应用并由云端推送到手机端
     * @param request
     * @param callback
     * @return
     */
    public void indexSearch(AppSearchRequest request, ResultCallBack<Result<CommentPage<SearchResultEntity>>> callback){
        observableService.execute(iCloudCommentService.indexSearch(request), new DisposableObserver<Result<CommentPage<SearchResultEntity>>>(){

            @Override
            public void onNext(@NotNull Result<CommentPage<SearchResultEntity>> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 分页显示安利墙列表
     * @param request
     * @param callback
     */
    public void commentInitAmwayWallList(RecommendListRequest request, ResultCallBack<Result<RecommendListEntity>> callback){
        observableService.execute(iCloudCommentService.commentInitAmwayWallList(request), new DisposableObserver<Result<RecommendListEntity>>(){

            @Override
            public void onNext(@NotNull Result<RecommendListEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 应用评论列表
     * @param request
     * @param callback
     */
    public void commentCommentList(CommentListRequest request, ResultCallBack<Result<CommentListEntity>> callback){
        observableService.execute(iCloudCommentService.commentList(request), new DisposableObserver<Result<CommentListEntity>>(){

            @Override
            public void onNext(@NotNull Result<CommentListEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }

    /**
     * 用户评论列表
     * @param request
     * @param callback
     */
    public void commentUserCommentList(CommentListRequest request, ResultCallBack<Result<CommentListEntity>> callback){
        observableService.execute(iCloudCommentService.commentUserCommentList(request), new DisposableObserver<Result<CommentListEntity>>(){

            @Override
            public void onNext(@NotNull Result<CommentListEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }
    
    /**
     * 显示app总体评分和所有用户评价列表
     * @param request
     * @param callback
     */
    public void commentScore(CommentScoreRequest request, ResultCallBack<Result<AppCommentScoreEntity>> callback){
        observableService.execute(iCloudCommentService.commentScore(request), new DisposableObserver<Result<AppCommentScoreEntity>>(){

            @Override
            public void onNext(@NotNull Result<AppCommentScoreEntity> o) {
                if (callback != null) {
                    callback.onSuccess(o);
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onError(@NotNull Throwable e) {
                if (e == null) {
                    e = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
                }
                if (callback != null) {
                    callback.onError("", e.getMessage());
                }
                if (!isDisposed()) {
                    dispose();
                }
            }

            @Override
            public void onComplete() {
                if (!isDisposed()) {
                    try {
                        callback.onComplete();
                    } finally {
                        dispose();
                    }
                }
            }
        });
    }



    /**
     * 显示app总体评分和所有用户评价列表
     * @param request
     * @param callback
     */
    public void commentCommentDetail(CommentDetailRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.commentDetail(request), callback);
    }

    /**
     * 下载用户对app进行评价及安利
     * @param request
     * @param callback
     */
    public void commentEditComment(BaseRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.commentEditComment(request), callback);
    }

    /**
     * 下载用户对app下的用户评论进行回复
     * @param request
     * @param callback
     */
    public void commentEditReply(BaseRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.commentEditReply(request), callback);
    }

    /**
     * 对用户的评论进行点赞
     * @param request
     * @param callback
     */
    public void commentCommentPraise(BaseRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.commentCommentPraise(request), callback);
    }

    /**
     * 用户评论审核不通过，可对当前评论进行修改
     * @param request
     * @param callback
     */
    public void commentInitCommentDetail(BaseRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.commentInitCommentDetail(request), callback);
    }

    /**
     * 用户评论审核不通过，可对当前评论进行修改
     * @param request
     * @param callback
     */
    public void commentUpdateComment(BaseRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.commentUpdateComment(request), callback);
    }





    /**
     * 用户删除自己的评论
     * @param request
     * @param callback
     */
    public void commentDelUserComment(BaseRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.commentDelUserComment(request), callback);
    }

    /**
     * 查询应用下评论数，不包含回复和回复回复
     * @param request
     * @param callback
     */
    public void commentSearchCommentCount(BaseRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.commentSearchCommentCount(request), callback);
    }

    /**
     * 用户提交意见反馈
     * @param request
     * @param callback
     */
    public void feedbackFeedbackCommit(BaseRequest request, ResultCallBack<Result> callback){
        innerExecute(iCloudCommentService.feedbackFeedbackCommit(request), callback);
    }
    
}


